home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / NRCOBOL1g / Extra / Filter.c < prev    next >
C/C++ Source or Header  |  1998-09-22  |  6KB  |  228 lines

  1. /* Filter.c  program to read MSdos files and produce Amiga files 19/3/97 */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <libraries/dos.h>
  6. #include <intuition/intuition.h>
  7. #include <workbench/startup.h>         /*   */
  8. #include <workbench/workbench.h>
  9. #include <utility/tagitem.h>
  10. #include <utility/hooks.h>
  11. #include <libraries/asl.h>
  12.  
  13. char VERSTAG[]="\0$VER: asltest 37.2";
  14.  
  15. /***********************************************************************/
  16.  
  17. struct Library  *AslBase;
  18. struct FileRequester *FileRequester;
  19. struct FileRequester *FileSave;
  20. #define maxline 220
  21.  
  22. /************************************************************************/
  23.  
  24.  
  25. struct TagItem MultiTags[] = {
  26.     ASL_FuncFlags,FILF_PATGAD,
  27.     ASL_Pattern,(ULONG)"#?.FD",
  28.     ASL_Hail,(ULONG)"Select file to convert",
  29.     TAG_DONE,
  30.     };
  31.  
  32. struct TagItem MultiTagread[] = {
  33.     ASL_FuncFlags,FILF_PATGAD,
  34.     ASL_Pattern,(ULONG)"#?",
  35.     ASLFR_TitleText,(ULONG)"Select File to filter",
  36.    ASLFR_RejectIcons,TRUE,
  37.    TAG_DONE,TAG_DONE
  38.     };
  39.  
  40. struct TagItem MultiTagsave[] = {
  41.     ASL_FuncFlags,FILF_PATGAD,
  42.     ASL_Pattern,(ULONG)"#?",
  43.     ASLFR_TitleText,(ULONG)"Save FIltered file as..",
  44.    ASLFR_DoSaveMode,TRUE,
  45.    ASLFR_RejectIcons,TRUE,
  46.    ASLFR_InitialFile,(ULONG)".flt",
  47.    TAG_DONE,TAG_DONE
  48.     };
  49.  
  50.  
  51. #define false 0
  52. #define true !false
  53. struct DiskObject *mydiskinfo;
  54. LONG IconBase,_fromWB=true;
  55. char found[maxline];
  56. /************************************************************************/
  57.  
  58. int main(argc,argv)
  59. int argc;
  60. char *argv[];
  61. {
  62. char infilename[maxline],outfilename[maxline],
  63.      progname[maxline],filtstr[maxline];
  64. struct WBArg *wb_arg;
  65. struct WBStartup *argmsg;
  66. int  filtval=7;
  67.  
  68.  
  69.   if(argc == 0 && argv) _fromWB=true;
  70.  
  71.   if (_fromWB){
  72.      argmsg=(struct WBStartup *)argv;
  73.      wb_arg=(struct WBArg *)argmsg->sm_ArgList;
  74.      if (*wb_arg->wa_Name)
  75.     strcpy(&progname[0],(char *)wb_arg->wa_Name);
  76.      else
  77.         strcpy(&progname[0],"Filter");
  78.  
  79.      if (OpenLibrary("intuition.library",0L)){
  80.     if ((IconBase=(LONG)OpenLibrary("icon.library",0L))){
  81.  
  82.       strcpy(filtstr,"");
  83.           if ((mydiskinfo=(struct DiskObject *)GetDiskObject(&progname[0]))){
  84.          strcpy(&filtstr[0],(char *)FindToolType(mydiskinfo->do_ToolTypes,"FILTER"));
  85.          FreeDiskObject(mydiskinfo);
  86.              sscanf(&filtstr[0],"%d",&filtval);
  87.           }
  88.           CloseLibrary(IconBase);
  89.         }
  90.  
  91.         if ((AslBase = (struct Library  *)OpenLibrary(AslName,36)) != 0 ) {
  92.            FileRequester = (struct FileRequester *)AllocAslRequest(ASL_FileRequest, NULL);
  93.            if (FileRequester != NULL){
  94.               FileSave = (struct FileRequester *)AllocAslRequest(ASL_FileRequest, NULL);
  95.               if (FileSave != NULL){
  96.  
  97.                  while(AslRequest(FileRequester,MultiTagread)) {
  98.                     Parsename(FileRequester,&infilename[0]);
  99.                     if (AslRequest(FileSave,MultiTagsave)){
  100.                        Parsename(FileSave,&outfilename[0]);
  101.                        if (strcmp(infilename,outfilename))
  102.                           repeat_work(infilename,outfilename,(char)filtval);
  103.                        else
  104.                          printf("whoops, both names are same!\n");
  105.                     }
  106.              }
  107.  
  108.                  FreeAslRequest(FileSave);
  109.               }
  110.               FreeAslRequest(FileRequester);
  111.            }else
  112.            printf("Failed to allocate file request\n");
  113.  
  114.            CloseLibrary(AslBase);
  115.         }else
  116.         printf("Can't open asl.library\n");
  117.       }
  118.    }
  119.    else
  120.       if (argc==3){
  121.          strcpy(infilename,argv[1]);
  122.          strcpy(outfilename,argv[2]);
  123.          sscanf(argv[3],"%d",&filtval);
  124.          repeat_work(infilename,outfilename,(char)filtval);
  125.       }else
  126.           printf("Usage: Filter <infile> <outfile> <filter>\n");
  127.  
  128.    exit(0);
  129.    return(0);
  130. }
  131.  
  132. /* ------------------------------------------------------------------- */
  133. Parsename(FileReq,name)
  134. struct FileRequester *FileReq;
  135. char *name;
  136. {
  137. char filestring[maxline];
  138.  
  139.     if (strlen(FileReq->rf_Dir)){
  140.        if (FileReq->rf_Dir[strlen(FileReq->rf_Dir)-1]==':')
  141.           sprintf(&filestring[0],"%s%s",FileReq->rf_Dir,FileReq->rf_File);
  142.        else
  143.           sprintf(&filestring[0],"%s/%s",FileReq->rf_Dir,FileReq->rf_File);
  144.     }else
  145.         sprintf(&filestring[0],"%s",FileReq->rf_File);
  146.     strcpy(name,&filestring[0]);
  147.  
  148. }
  149. /* ------------------------------------------------------------------- */
  150. repeat_work(infilename,outfilename,filtval)
  151. char *infilename,*outfilename,filtval;
  152. {
  153. FILE *infile,*outfile;
  154. char *inbuffer,*buffer;
  155. int  exit_loop=0,offset=0;
  156. long filesize=0;
  157.  
  158.       if ((infile=fopen(infilename,"r"))){
  159.          if ((outfile=fopen(outfilename,"w"))){
  160.             printf("Reading file '%s' with filter %d.....\n",infilename,filtval);
  161.             filesize=get_filesize(infile);
  162.             if (buffer =malloc(filesize)){
  163.                inbuffer=buffer;
  164.                fread(inbuffer,filesize,1,infile);
  165.                do{
  166.                    if (*inbuffer!=filtval)
  167.                       fwrite(inbuffer,1,1,outfile);
  168.                    inbuffer++;
  169.                    offset++;
  170.  
  171.                }while(offset<filesize);
  172.                free(buffer);
  173.             }
  174.             fclose(outfile);
  175.             printf("Filter compleate.\n");
  176.          }else
  177.              printf("Could not open file '%s'\n",outfilename);
  178.          fclose(infile);
  179.       }else
  180.           printf("Could not open file '%s'\n",infilename);
  181.  
  182. }
  183. /* ======================================================================= */
  184. FindTooltype(list,find)
  185. char **list;
  186. char *find;
  187. {
  188. char temp[maxline];
  189. int  index=0,index2=0;
  190.  
  191.    while(list[index])
  192.    {
  193.       strcpy(&temp[0],list[index]);
  194.       temp[strlen(find)]=0;           /* just start of tooltype */
  195.  
  196.       while(temp[index2])
  197.       {
  198.      temp[index2]=toupper(temp[index2]);
  199.      index2++;
  200.       }
  201.  
  202.       if (strcmp(&temp[0],find)==0)
  203.       {
  204.      strcpy(&temp[0],list[index]);
  205.      if (strlen(temp)>strlen(find))
  206.      {
  207.         strcpy(&found[0],&temp[strlen(find)+1]);
  208.      }
  209.      return ((char *)&found[0]);
  210.       }
  211.       index++;
  212.    }
  213.    return (0);
  214. }
  215. /* ======================================================================= */
  216. get_filesize(filepointer)
  217. FILE *filepointer;
  218. {
  219. long origpos,sizepos;
  220.  
  221.    origpos=ftell(filepointer);
  222.    fseek(filepointer,0,SEEK_END);                /* go to end of file */
  223.    sizepos=ftell(filepointer);
  224.    fseek(filepointer,origpos,SEEK_SET);  /* report position, ie size */
  225.    return(sizepos);
  226. }
  227. /* ====================================================================== */
  228.